<?php

namespace Liuhelong\LaravelAdmin\Wechat\Http\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountEventFunction;
use Log;
use Liuhelong\LaravelAdmin\Wechat\Common;

class WechatOffiaccountEventFunctionController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '事件处理';
    private $event_types = [
        'subscribe'=>'关注',
		'unsubscribe'=>'取消关注',
		'SCAN'=>'扫码',
		'CLICK'=>'菜单-点击',
		'VIEW'=>'菜单-点击菜单跳转链接',
		'view_miniprogram'=>'菜单-点击菜单跳转小程序',
		'scancode_push' => '菜单-扫码推送',
		'scancode_waitmsg' => '菜单-扫码等待',
		'pic_sysphoto' => '菜单-拍照',
		'pic_weixin' => '菜单-相册',
		'pic_photo_or_album' => '菜单-拍照/相册',
		'location_select' => '菜单-位置',
		'LOCATION' => '上报地理位置事件',
		'MASSSENDJOBFINISH' =>'群发消息成功回调',  
    ];
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new WechatOffiaccountEventFunction());

        $grid->column('id', __('laravel_admin_wechat.Id'))->hide();
        $grid->column('name', __('laravel_admin_wechat.Name'));
        $grid->column('type', __('laravel_admin_wechat.Type'))->using($this->event_types);
		
        $grid->column('key_type', __('laravel_admin_wechat.Key type'))->using([
			'equal'=>'全匹配',
			'like'=>'简单匹配',
			'reg'=>'正则匹配',
		]);;
        $grid->column('key', __('laravel_admin_wechat.Key'));
        $grid->column('reply_type', __('laravel_admin_wechat.Reply type'))->using([
			'text'=>'文字',
			'image'=>'图片',
			'voice'=>'语音',
			'video'=>'视频',
			'news'=>'图文消息'
		]);
        $grid->column('reply_content', __('laravel_admin_wechat.Reply content'))->display(function($reply_content,$column){
            
            return Common::getFile($this->reply_type,$reply_content);
        })->width(300);
        $grid->column('function', __('laravel_admin_wechat.Function'));
        $grid->column('cs_messages', __('laravel_admin_wechat.Cs messages'))->display(function($cs_messages){
            return collect($cs_messages)->map(function($item){
                return $item['name'];
            });
        })->label();
        $grid->column('status', __('laravel_admin_wechat.Status'))->switch();
        $grid->column('created_at', __('laravel_admin_wechat.Created at'))->hide();
        $grid->column('updated_at', __('laravel_admin_wechat.Updated at'))->hide();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(WechatOffiaccountEventFunction::findOrFail($id));

        $show->field('id', __('laravel_admin_wechat.Id'));
        $show->field('name', __('laravel_admin_wechat.Name'));
        $show->field('type', __('laravel_admin_wechat.Type'));
        $show->field('key', __('laravel_admin_wechat.Key'));
        $show->field('reply_type', __('laravel_admin_wechat.Reply type'));
        $show->field('reply_content', __('laravel_admin_wechat.Reply content'));
        $show->field('function', __('laravel_admin_wechat.Function'));
        $show->field('status', __('laravel_admin_wechat.Status'));
        $show->field('created_at', __('laravel_admin_wechat.Created at'));
        $show->field('updated_at', __('laravel_admin_wechat.Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WechatOffiaccountEventFunction());
        $form->text('name', __('laravel_admin_wechat.Name'));

        $form->select('type', __('laravel_admin_wechat.Type'))->options($this->event_types)
		->help('可以配合关键词匹配，进行精准事件触发/回复；只有部分事件支持内容回复，详情参考 https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_event_pushes.html')
		->when('in', ['subscribe','SCAN','CLICK','LOCATION','VIEW'], function (Form $form) {
		    
            $form->select('reply_type', __('laravel_admin_wechat.Reply type'))->options([
    			'text'=>'文字',
    			'image'=>'图片',
    			'voice'=>'语音',
    			'video'=>'视频',
    			'news'=>'图文消息'
    		])
    		->when('text', function (Form $form) { 
    
    			$form->textarea('text', __('laravel_admin_wechat.Reply content'));
    
    		})
    		->when('in',['image','voice','video'], function (Form $form) { 
    
    			$form->text('reply_media', __('laravel_admin_wechat.Reply media'))->help('请输入素材库中相应素材的资源id');
    
    		})
    		->when('news', function (Form $form) { 
    
    			$form->select('news', __('laravel_admin_wechat.Reply content'))->options(\Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountNew::all()->pluck('title','id')->all());
    
    		});
    		
        });
        
		
        $form->text('function', __('laravel_admin_wechat.Function'))
        ->help('附加事件方法 namespace\class@method');
        
        $form->checkbox('cs_messages', __('laravel_admin_wechat.Cs messages'))
        ->options(\Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountCsMessage::all()->pluck('name','id')->all())
        ->help('附加的客服消息回复，仅部分事件支持，详细请看文档');
        
        $form->switch('status', __('laravel_admin_wechat.Status'))->default(1);

        $form->fieldset('关键词处理(可选)', function (Form $form) {
            $form->select('key_type', __('laravel_admin_wechat.Key type'))->options([
    			'equal'=>'全匹配',
    			'like'=>'简单匹配',
    			'reg'=>'正则匹配',
    		])->help('简单匹配:foo* 匹配 footer');
            $form->text('key', __('laravel_admin_wechat.Key'))->help('正则匹配时这里输入正则表达式(如：/^TEST$/)');
        });
        return $form;
    }
	
}
